package huoye.shops.base;

import android.app.ActivityManager;
import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Process;
import android.support.multidex.MultiDex;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

import com.codeideology.android.AXIApplication;
import com.codeideology.android.http.okhttp.OkHttpUtils;
import com.codeideology.android.http.okhttp.cookie.CookieJarImpl;
import com.codeideology.android.http.okhttp.cookie.store.PersistentCookieStore;
import com.codeideology.android.http.okhttp.https.HttpsUtils;
import com.codeideology.android.http.okhttp.interceptor.BasicParamsInterceptor;
import com.codeideology.android.http.okhttp.log.LoggerInterceptor;
import com.codeideology.android.utils.AXIApplicationPreference;
import com.codeideology.android.utils.AndroidUtils;
import com.codeideology.android.utils.ApkUtils;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.MsgConstant;
import com.umeng.message.PushAgent;
import com.umeng.message.UTrack;
import com.umeng.message.UmengMessageHandler;
import com.umeng.message.UmengNotificationClickHandler;
import com.umeng.message.entity.UMessage;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import huoye.shops.guidance.LaunchActivity;
import huoye.shops.sp.SharedPreferencesHelper;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static anet.channel.util.Utils.context;

/**
 * Created by user on 2017/7/24.
 */

public class FSApplication extends AXIApplication {
    private static FSApplication instance;
    private String uid;

    public static FSApplication getInstance() {
        return instance;
    }

    public FSApplication() {
    }

    private SharedPreferencesHelper preference;

    ApplicationHelper helper;

    private static final String APP_ID = "2882303761517612225";
    private static final String APP_KEY = "5891761229225";

    @Override
    protected boolean isCrashHandler() {
        return false;
    }


    public TokenInterceptor getInterceptor() {
        return interceptor;
    }

    public boolean isUpdate() {
        return update;
    }

    public void setUpdate(boolean update) {
        this.update = update;
    }

    private boolean update = true;
    private TokenInterceptor interceptor;


    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        MultiDex.install(this);
        preference = new SharedPreferencesHelper(this);
        helper = ApplicationHelper.getInstance();
        helper.setAxiPreference(preference);
        helper.setApplication(this);

        //绑定zxing 扫码
        ZXingLibrary.initDisplayOpinion(this);

        //绑定友盟推送
        //初始化组件化基础库, 统计SDK/推送SDK/分享SDK都必须调用此初始化接口
        UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, "a794b97b85eaaf11707f9c9ebe8e3bcb");
        initUpush();

        UserManager.getInstance().setPreference(preference);
        UserManager.getInstance().setDeviceHash(AndroidUtils.getUUID(this));
        String type = android.os.Build.MODEL;

       interceptor = new TokenInterceptor(this);





     /*  OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(new LoggerInterceptor("TAG"))
              *//*  .addNetworkInterceptor(new Interceptor() {//自定义拦截器
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request request  = chain.request();
                        //配置统一的头
                        Request  newRequest   = request.newBuilder().addHeader("head", "xiayu").build();
                        return  chain.proceed(newRequest);
                    }
                })*//*
                .addNetworkInterceptor(interceptor)//增加Stetho拦截器,用于调试
                .addInterceptor(new Dig())
                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                .readTimeout(10000L, TimeUnit.MILLISECONDS)
                //其他配置
                .build();
        //使用自定义OkHttpClient
        OkHttpUtils.initClient(okHttpClient);*/














      BasicParamsInterceptor basicParamsInterceptor =
                new BasicParamsInterceptor.Builder()
                   /*     .addParam("version", ApkUtils.getVersionCode(this) + "")
                        .addParam("device_sn", AndroidUtils.getUUID(this))
                        .addParam("device_type", type)
                        .addParam("os_type", "2")*/
                        .build();
        HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null,null, null);

        CookieJarImpl cookieJar = new CookieJarImpl(new PersistentCookieStore(getApplicationContext()));
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(new LoggerInterceptor("TAG"))
                .addInterceptor(interceptor)
                .addInterceptor(basicParamsInterceptor)
                .addInterceptor(new Dig())
                .connectTimeout(6, TimeUnit.SECONDS)
                .readTimeout(6, TimeUnit.SECONDS)
                .cookieJar(cookieJar)
               // .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
                //其他配置
                .build();
         OkHttpUtils.initClient(okHttpClient);

         initShare();

    }

    private void initShare(){

    /*    UMConfigure.init(this,"5b42c1b9f43e485bda00004b","umeng",UMConfigure.DEVICE_TYPE_PHONE,"");
        PlatformConfig.setWeixin("wx4579a88f8d6b8c82", "e6b0fbc604f195d0dd1df04ba8c0c97e");
        //豆瓣RENREN平台目前只能在服务器端配置
        PlatformConfig.setQQZone("1106954293", "4ejNYGRA0h8wH8Yl");*/
    }


    private boolean shouldInit() {
        ActivityManager am = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE));
        List<ActivityManager.RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
        String mainProcessName = getPackageName();
        int myPid = Process.myPid();
        for (ActivityManager.RunningAppProcessInfo info : processInfos) {
            if (info.pid == myPid && mainProcessName.equals(info.processName)) {
                return true;
            }
        }
        return false;
    }



    private void registToWX() {


    }


    private void initUpush() {
        PushAgent mPushAgent = PushAgent.getInstance(this);
         //注册推送服务，每次调用register方法都会回调该接口
        mPushAgent.register(new IUmengRegisterCallback() {

            @Override
            public void onSuccess(String deviceToken) {
                //注册成功会返回device token
            }

            @Override
            public void onFailure(String s, String s1) {

            }
        });




    }




    @Override
    protected Class<?> getLaunchActivity() {
        return LaunchActivity.class;
    }


}
